//
//  LYQTopicListSelectedView.m
//  ZCProject
//
//  Created by Zheng on 2016/12/20.
//  Copyright © 2016年 Zheng. All rights reserved.
//

#import "LYQTopicListSelectedView.h"

@implementation LYQTopicListSelectedView

- (instancetype)init {
    self = [super init];
    if (self) {
        
        [self addSubview:self.newsBtn];
        [self addSubview:self.focusBtn];
        [self addSubview:self.lineView];
        [self addSubview:self.line2View];
        [self addSubview:self.selectedView];
        
        [self layoutSubviewsInView];
        
    }
    return self;
}

#pragma mark - layout subviews
- (void)layoutSubviewsInView {
    
    [_newsBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self);
        make.right.equalTo(self.mas_centerX).offset(-0.25);
    }];
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_newsBtn.mas_right);
        make.top.bottom.equalTo(_newsBtn);
        make.width.equalTo(@0.5);
    }];
    
    [_focusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_lineView.mas_right);
        make.top.bottom.right.equalTo(self);
    }];
    
    [_line2View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self);
        make.height.equalTo(@0.5);
    }];

}

#pragma mark - lazy load subviews
- (UIButton *)newsBtn {
    if (_newsBtn) {
        return _newsBtn;
    }
    
    _newsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_newsBtn setTitle:@"最新" forState:UIControlStateNormal];
    [_newsBtn setTitleColor:kHexadecimal(@"666666") forState:UIControlStateNormal];
    [_newsBtn setTitle:@"最新" forState:UIControlStateSelected];
    [_newsBtn setTitleColor:kLYQBlue forState:UIControlStateSelected];
    _newsBtn.titleLabel.font = kWFont(17);
    _newsBtn.selected = YES;
    [_newsBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    _selectedBtn = _newsBtn;
    
    return _newsBtn;
}

- (UIButton *)focusBtn {
    if (_focusBtn) {
        return _focusBtn;
    }
    
    _focusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_focusBtn setTitle:@"关注" forState:UIControlStateNormal];
    [_focusBtn setTitleColor:kHexadecimal(@"666666") forState:UIControlStateNormal];
    [_focusBtn setTitle:@"关注" forState:UIControlStateSelected];
    [_focusBtn setTitleColor:kLYQBlue forState:UIControlStateSelected];
    _focusBtn.titleLabel.font = kWFont(17);
    _focusBtn.selected = NO;
    [_focusBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    return _focusBtn;
}

- (UIView *)lineView {
    
    if (_lineView) {
        return _lineView;
    }
    
    _lineView = [[UIView alloc] init];
    _lineView.backgroundColor = RGB(200, 199, 204, 1);
    
    return _lineView;
}

- (UIView *)line2View {
    
    if (_line2View) {
        return _line2View;
    }
    
    _line2View = [[UIView alloc] init];
    _line2View.backgroundColor = RGB(200, 199, 204, 1);
    
    return _line2View;
}

- (UIView *)selectedView {
    if (_selectedView) {
        return _selectedView;
    }
    
    _selectedView = [[UIView alloc] initWithFrame:CGRectMake(kWScale(55), kWScale(47), kWScale(80), kWScale(3))];
    _selectedView.backgroundColor = kLYQBlue;
    
    return _selectedView;
}

#pragma mark - 点击事件
- (void)buttonAction:(UIButton *)sender {
    
    
    if (_selectedBtn != sender) {
        
        CGRect frame;
        
        if (sender == _newsBtn) {
            _block(0);
            
            frame = CGRectMake(kWScale(55), kWScale(47), kWScale(80), kWScale(3));
            
        } else {
            _block(1);
            
            frame = CGRectMake(kMS_Width-kWScale(130), kWScale(47), kWScale(80), kWScale(3));
        }
        
        [UIView animateWithDuration:0.35 animations:^{
            _selectedView.frame = frame;
        }];
        
        _selectedBtn.selected = !_selectedBtn.selected;
        _selectedBtn = sender;
        _selectedBtn.selected = !_selectedBtn.selected;
    }
    
}

- (void)chooseButtonTypeWithChooseBlock:(ChooseBlock)block {
    _block = block;    
}



@end
